<?php
/**
 * Created by PhpStorm.
 * User: yangwang
 * Date: 2024-04-24
 * Time: 16:03
 */

namespace App\Kuafu\Shop\Service;


use App\Exceptions\BaseException;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Shop\Model\GuanyuanSyncShopListModel;

class GuanyuanSyncShopListService extends BaseService
{
    public static function batchInsertData($list)
    {
        if (empty($list)) {
            throw new BaseException(BaseCodeEnum::ERR_400000, '数据为空');
        }

        $insertData = [];
        foreach ($list as $item){
            $insertData[] = [
                'region'                  => (string)($item['region'] ?? ''),
                'operationManager'        => (string)($item['operationManager'] ?? ''),
                'privateDomain'           => (string)($item['privateDomain'] ?? ''),
                'takeawayStartTime'       => (string)($item['takeawayStartTime'] ?? ''),
                'code'                    => (int)($item['code'] ?? 0),
                'name'                    => (string)($item['name'] ?? ''),
                'status'                  => (string)($item['status'] ?? ''),
                'businessType'            => (string)($item['businessType'] ?? ''),
                'storeType'               => (string)($item['storeType'] ?? ''),
                'cashierSystem'           => (string)($item['cashierSystem'] ?? ''),
                'province'                => (string)($item['province'] ?? ''),
                'city'                    => (string)($item['city'] ?? ''),
                'district'                => (string)($item['district'] ?? ''),
                'belongBranch'            => (string)($item['belongBranch'] ?? ''),
                'address'                 => (string)($item['address'] ?? ''),
                'contractStartDate'       => (string)($item['contractStartDate'] ?? ''),
                'contractEndDate'         => (string)($item['contractEndDate'] ?? ''),
                'openBusinessDate'        => (string)($item['openBusinessDate'] ?? ''),
                'closeDate'               => (string)($item['closeDate'] ?? ''),
                'franchiseFee'            => (float)($item['franchiseFee'] ?? 0),
                'managementFee'           => (float)($item['managementFee'] ?? 0),
                'transferFee'             => (float)($item['transferFee'] ?? 0),
                'area'                    => (float)($item['area'] ?? 0),
                'rent'                    => (float)($item['rent'] ?? 0),
                'artificial'              => (float)($item['artificial'] ?? 0),
                'energy'                  => (float)($item['energy'] ?? 0),
                'employeesNum'            => (int)($item['employeesNum'] ?? 0),
                'operationSupervision'    => (string)($item['operationSupervision'] ?? ''),
                'developmentManager'      => (string)($item['developmentManager'] ?? ''),
                'siteLocationType'        => (string)($item['siteLocationType'] ?? ''),
                'inputPrice'              => (float)($item['inputPrice'] ?? 0),
                'phone'                   => (string)($item['phone'] ?? ''),
                'degreeOfCooperation'     => (string)($item['degreeOfCooperation'] ?? ''),
                'franchiseeRealName'      => (string)($item['franchiseeRealName'] ?? ''),
                'franchiseeName'          => (string)($item['franchiseeName'] ?? ''),
                'franchiseConsultant'     => (string)($item['franchiseConsultant'] ?? ''),
                'preparatoryConsultant'   => (string)($item['preparatoryConsultant'] ?? ''),
                'preparatoryFinishDate'   => (string)($item['preparatoryFinishDate'] ?? ''),
                'preparatoryStartDate'    => (string)($item['preparatoryStartDate'] ?? ''),
                'preparatoryTimeConsumed' => (int)($item['preparatoryTimeConsumed'] ?? 0),
                'openShopTimeConsumed'    => (int)($item['openShopTimeConsumed'] ?? 0),
                'lng'                     => (float)($item['lng'] ?? 0),
                'lat'                     => (float)($item['lat'] ?? 0),
                'designConsultant'        => (string)($item['designConsultant'] ?? ''),
                'designStartDate'         => (string)($item['designStartDate'] ?? ''),
                'designEndDate'           => (string)($item['designEndDate'] ?? ''),
                'currentStatus'           => (string)($item['currentStatus'] ?? ''),
                'currentNodeName'         => (string)($item['currentNodeName'] ?? ''),
                'estimatedOpeningTime'    => (string)($item['estimatedOpeningTime'] ?? ''),
                'relocationMark'          => (string)($item['relocationMark'] ?? ''),
                'keyCity'                 => (string)($item['keyCity'] ?? ''),
                'subStatus'               => (string)($item['subStatus'] ?? ''),
                'earliestRevenueDate'     => (string)($item['earliestRevenueDate'] ?? ''),
                'orderNo'                 => (string)($item['orderNo'] ?? ''),
                'confirmSiteDate'         => (string)($item['confirmSiteDate'] ?? ''),
                'shopGrade'               => (string)($item['shopGrade'] ?? ''),
                'actualIncome'            => (float)($item['actualIncome'] ?? 0),
                'createdAt'               => (int)($item['createdAt'] ?? 0),
                'updatedAt'               => (int)($item['updatedAt'] ?? 0),
            ];
        }

        if (empty($insertData)){
            throw new BaseException(BaseCodeEnum::ERR_400000, '数据结构异常');
        }


        GuanyuanSyncShopListModel::query()
                                 ->whereIn('code', array_column($insertData, 'code'))
                                 ->delete();
        

        foreach (array_chunk($insertData, 50) as $insertChunk) {
            GuanyuanSyncShopListModel::query()->insert($insertChunk);
        }

        return true;
    }

}